package com.grid.camera.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class ClientChannelInitializer extends ChannelInitializer {

    private final NettyClient nettyClient;

    public ClientChannelInitializer(NettyClient NettyClient) {
        this.nettyClient = NettyClient;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        // 心跳检测
        pipeline.addLast(new IdleStateHandler(0, 30, 0, TimeUnit.SECONDS));
        // 字符串编码（默认编码）
        pipeline.addLast("encoder", new StringEncoder());
        // 客户端的逻辑
        pipeline.addLast("handler", new NettyClientHandler(nettyClient));
    }
}
